const { mock } = require('mockjs')
const accessTokens = {
  admin: 'admin-accessToken',
  editor: 'editor-accessToken',
  test: 'test-accessToken',
}
const menus = []
for (let i = 1; i <= 10; i++) {
  let pObj = mock({
    id: 'p' + i,
    title: '@title(1, 2)',
    children: [],
  })
  for (let j = 1; j <= 5; j++) {
    pObj.children.push(
      mock({
        id: 'c' + i + j,
        title: '@title(1, 2)',
      })
    )
  }
  menus.push(pObj)
}

module.exports = [
  {
    url: '/user/login',
    type: 'post',
    response(config) {
      const { username } = config.body
      const accessToken = accessTokens[username]
      if (!accessToken) {
        return {
          code: 500,
          msg: '帐户或密码不正确。',
        }
      }
      return {
        code: 200,
        msg: 'success',
        data: { accessToken },
      }
    },
  },
  {
    url: '/user/logout',
    type: 'post',
    response() {
      return {
        code: 200,
        msg: 'success',
      }
    },
  },
  {
    url: '/user/getUserinfo',
    type: 'get',
    response(config) {
      const { accessToken } = config.query
      console.log('获取' + accessToken + '的用户信息')
      return {
        code: 200,
        msg: 'success',
        userInfo: mock({
          name: '@cname',
        }),
      }
    },
  },
  {
    url: '/user/getMenus',
    type: 'get',
    response(config) {
      const { accessToken } = config.query
      console.log('获取' + accessToken + '的菜单')
      return {
        code: 200,
        msg: 'success',
        menus: menus,
      }
    },
  },
]
